//
//  TableViewModel+ImagesRows.swift
//  CooperSwift
//
//  Created by 计海峰 on 2022/1/17.
//

import Foundation

// 通用 rows 的一些扩展
public extension TableViewModelProtocol {

    // MARK: - single image show
    /// isDelay true 需要 实现 lazyload
    func imageConfig(_ url: String, padding: UIEdgeInsets = UIEdgeInsets(top: 0.01, left: 12, bottom: 0.01, right: 12), masksToBounds: Bool = true, radius: CGFloat = 8, isDelay: Bool = false) -> TableShowImageConfig {
        let singleModel = TableShowImageConfig().then {
            /// 图片路径
            $0.url = url
            /// 边距
            $0.vTopCons = padding.top
            $0.vLeftCons = padding.left
            $0.vBottomCons = -padding.bottom
            $0.vRightCons = -padding.right
            /// 最大宽度
            $0.vMaxWidthCons = Screen.width - padding.left - padding.right /// - 44
            /// 一些基本配置
            $0.masksToBounds = masksToBounds
            $0.vRadius = radius
            $0.isDelay = isDelay
        }
        return singleModel
    }
    
    /// image show
    func imageShowRow(_ config: TableShowImageConfig, key: String, isTarget: Bool = true) -> TableShowImageCellModel {
        let singleImageCellModel = TableShowImageCellModel.init(config).then {
            $0.addTargetKey(key, isTarget: isTarget)
            $0.cellHeight = 0.01
        }
        return singleImageCellModel
    }
    
    // MARK: - mult image show
    /// images item config
    func imagesConfig(_ url: String, vTopCons: CGFloat = 8, vLeftCons: CGFloat = 8, vWidthCons: CGFloat = (Screen.width - 32.0)/3.0 - 0.01, vHeightCons: CGFloat = (Screen.width - 32.0)/3.0 - 0.01, masksToBounds: Bool = true, radius: CGFloat = 8) -> TableShowImageConfig {
        let singleModel = TableShowImageConfig().then {
            /// 图片路径
            $0.url = url
            /// 边距
            $0.vTopCons = vTopCons
            $0.vLeftCons = vLeftCons
            $0.vWidthCons = vWidthCons
            $0.vHeightCons = vHeightCons
            /// 一些基本配置
            $0.masksToBounds = masksToBounds
            $0.vRadius = radius
        }
        return singleModel
    }
    
    /// images views show
    func imagesShowByMultImageViewRow(_ configs: [TableShowImageConfig], isDelay: Bool = false, key: String, isTarget: Bool = true) -> TableShowImagesCellModel {
        var imagesModel = TableShowImagesConfig.init(configs)
        imagesModel.isDelay = isDelay
        let imagesCellModel = TableShowImagesCellModel.init(imagesModel).then {
            $0.cellHeight = imagesModel.cellHeight
        }
        return imagesCellModel
    }
    
    
    /// images collection show
    func flowLeftLayout(_ itemSize: CGSize, minLineSpace: CGFloat = 8.0, minInteritemSpac: CGFloat = 8.0 ) -> UICollectionViewFlowLayout {
        let flowLayout = UICollectionViewLeftAlignedLayout().then {
            $0.minimumLineSpacing = 8.0
            $0.minimumInteritemSpacing = 8.0
            $0.itemSize = itemSize
        }
        return flowLayout
    }
    
    func imagesShowByCollectionViewRow(_ configs: [CollectionCellConfigProtocol], cellHeight: CGFloat, layout: UICollectionViewFlowLayout? = nil, margin: UIEdgeInsets = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 12),  key: String, isTarget: Bool = true) -> TableShowCollectionViewCellModel {
        /// cvc 控制器 vm
        let cvcVM = CollectionViewBaseVM().then {
            $0.addFirstSectionItems(configs)
            $0.isLongPressGesture = false
            if let lay = layout {
                $0.flowLayout = lay
            }else {
                let width = (Screen.width - 16 - 24)/3.0
                $0.flowLayout = flowLeftLayout(CGSize(width, width))
            }
        }
        configs.forEach {
            if let vm = $0 as? BindKeyProtocol {
                vm.addTargetKey(cvcVM.bindKey(), isTarget: true)
            }
        }
        /// table -> cell config 配置
        let cellModel = TableShowCollectionViewCellModel(cvcVM).then {
            $0.cellHeight = cellHeight
            $0.addTargetKey(key, isTarget: isTarget)
            $0.margin = margin
        }
        return cellModel
    }
    
}
